﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Collections ;

using YT.Commons;
using YT.DB;


namespace YT.AdvanceCommons
{
    public class AdvanceFunction
    {

        private const int mintRowHeight = 20;
        private const int mintRowSpace = 10;
        private const int mintStandardSpace = 10;

        public static bool FillFormWithTitle(FlowLayoutPanel  pnlPage, string strMenuID)
        {
            AddItemToForm(pnlPage, strMenuID);
        

            return true;
        }

        private static bool AddItemToForm(FlowLayoutPanel pnlPage, string strMenuID)
        {
            int  lngX = 0;
            int  lngY = 0;

            string strSQL = string.Empty;
            string strHdrSeqNo = string.Empty;
            string strTitleTypeDesc = string.Empty;  //用于记录这题目的相关信息，用","号来分隔
            //分隔的次序是：MenuID, HdrSeqNo, SeqNo, 
            DBAccess dbcon = new DBAccess(false, false);
            strSQL = "select * from titlehdr where menuid='" + strMenuID + "' order by SeqNo ";
            DataTable dtTitleHdr = dbcon.GetDataTable(strSQL);
            foreach (DataRow drHdr in dtTitleHdr.Rows)
            {
                
                strHdrSeqNo = CvtFunc.ToString(drHdr["SeqNo"]);
                Panel pnlHdr = new Panel();
                pnlHdr.Width = pnlPage.Width;
                pnlHdr.Height = 30;
                strSQL = "select * from titleDtl where menuid='" + strMenuID + "' and SeqNo='"+ strHdrSeqNo +"' order by seqno";
                DataTable dtTitleDtl = dbcon.GetDataTable(strSQL);
                if (dtTitleDtl.Rows.Count == 0)  //如果沒有detail就是只head标题
                {
                    Label txtTitle = new Label();
                    txtTitle.BorderStyle = BorderStyle.None;                   
                    txtTitle.Text = CvtFunc.ToString(drHdr["Title"]);
                    txtTitle.AutoSize = true;
                    pnlHdr.Controls.Add(txtTitle);
                    pnlHdr.Height = txtTitle.Height;                    
                    lngX = 0; lngY = 0;
                    pnlPage.Controls.Add(pnlHdr);
                }
                else   //如果有,则是要填充题目。
                {
                    Control conLast = null;
                    Label txtTitle = new Label();        
                    txtTitle.Text = CvtFunc.ToString(drHdr["Title"]);
                    txtTitle.AutoSize = true;
                    txtTitle.Top = 0;
                    txtTitle.Left = 0;
                    pnlHdr.Controls.Add(txtTitle);                    
                    lngY = 0  ;
                    lngX = 0 ;
                    conLast = txtTitle;
                    foreach (DataRow drDtl in dtTitleDtl.Rows)
                    {
                        DtlTitleType dtlType = (DtlTitleType)CvtFunc.ToInt(drDtl["Type"]); 
                        bool blnNewRow= CvtFunc.ToBoolean(drDtl["NewRow"]); 
                        if (dtlType  == DtlTitleType.Single)  // 如果是单选类型
                        {
                            RadioButton radBut = new RadioButton();
                            radBut.Text = CvtFunc.ToString(drDtl["Description"]);
                            radBut.Tag  = CvtFunc.ToString(drDtl["ColTitle"]);
                            radBut.AutoSize = true;                            
                            SetControlPostion(pnlHdr, radBut, blnNewRow, ref  lngX, ref  lngY, conLast);
                            conLast = radBut;
                            pnlHdr.Controls.Add(radBut);
                            
                        }
                        else if ( dtlType == DtlTitleType.Mulit ) //如果是多选类型
                        {
                            CheckBox chkBox = new CheckBox();
                            chkBox.Text = CvtFunc.ToString(drDtl["Description"]);
                            chkBox.Tag = CvtFunc.ToString(drDtl["ColTitle"]);
                            chkBox.AutoSize = true;                            
                            SetControlPostion(pnlHdr, chkBox, blnNewRow, ref lngX, ref lngY, conLast);
                            conLast = chkBox;
                            pnlHdr.Controls.Add(chkBox);
                        }
                        else if (dtlType == DtlTitleType.Remark)  //如果是要填写的类型
                        {
                            //如果出现 "_____" 则表示这个地方要加 TextBox
                            string strRemark = CvtFunc.ToString(drDtl["Description"]);
                            string[] strArrRmk = BaseFunctions.Split(strRemark, "_____");
                            int intCout = strArrRmk.Length;
                            int intAdd = 0;
                            foreach (string strDtlRemark in strArrRmk)
                            {
                                intAdd++;
                                Label lblAdd = new Label();
                                lblAdd.Text = strDtlRemark;
                                lblAdd.AutoSize = true;                               
                                SetControlPostion(pnlHdr, lblAdd, blnNewRow, ref lngX, ref lngY, conLast);
                                conLast = lblAdd;
                                blnNewRow = false;
                                pnlHdr.Controls.Add(lblAdd);
                                if (intCout != intAdd)
                                {
                                    TextBox txtRemark = new TextBox();
                                    txtRemark.Text = "";
                                    txtRemark.Tag = CvtFunc.ToString(drDtl["ColTitle"]);
                                    txtRemark.BorderStyle = BorderStyle.None;
                                    txtRemark.Width = CvtFunc.ToInt(drDtl["Width"]); 
                                    SetControlPostion(pnlHdr, txtRemark, blnNewRow, ref lngX, ref lngY,conLast );
                                    conLast = txtRemark;
                                }
                            }                        
                        }
                    }
                    pnlPage.Controls.Add(pnlHdr);

                }
            }
            return true;
        }


        private static void SetControlPostion(Panel pnlHdr, Control con, bool blnNewRow,ref  int lngX,ref  int lngY , Control conLast)
        {
            if (blnNewRow == true)
            {
                lngY = lngY + mintRowHeight + mintRowSpace;
                pnlHdr.Height = pnlHdr.Height + mintRowHeight + mintRowSpace;
                lngX = 0;
            }
            else
            {
                lngX = lngX + conLast.Width + mintStandardSpace;
                pnlHdr.Width = pnlHdr.Width ;
            }
            con.Left = lngX;
            con.Top = lngY;
            pnlHdr.Controls.Add(con);
            
            //lngY = lngY + con.Height;
        }

        //private static bool AddItemToForm(DataRow drDtl)
        //{
        //    Panel panel1 = new Panel();
        //    TextBox textbox1 = new TextBox();
        //    textbox1.Name = "this is a test";
        //    textbox1.Text = "this is textbox";
        //    textbox1.Multiline = true;
        //    textbox1.BorderStyle = BorderStyle.None;
        //    RadioButton radBut = new RadioButton();
        //    radBut.Text = "This is a test! This is a test! This is a test! This is a test! This is a test! This is a test! This is a test! This is a test!";
        //    radBut.AutoSize = false ;
        //    radBut.Width = pnlPage.Width;
        //    textbox1.Width = pnlPage.Width;
        //    panel1.Width = pnlPage.Width;
        //    FlowLayoutPanel flyPanel = new FlowLayoutPanel();
        //    flyPanel.Width = pnlPage.Width;
        //    flyPanel.Controls.Add(textbox1);
        //    flyPanel.Controls.Add(radBut);

        //    RadioButton radBut2 = new RadioButton();
        //    radBut2.Text = "This is a test! This is a test! This is a test! This is a test! This is a test! This is a test! This is a test! This is a test!";
        //    radBut2.AutoSize = false;
        //    radBut2.Width = pnlPage.Width;
        //    flyPanel.Controls.Add(radBut2);
            
        //    panel1.Controls.Add(flyPanel);
        //    return true;
        //}

        

        private string GetFieldName(string con)
        {
            if (con.Length > 3)
            {
                return con.Substring(3, con.Length - 3);
            }
            return "";
        }
   
    }
}
